package com.wang.mall.controller;

import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wang.mall.bean.OmsOrder;
import com.wang.mall.service.OmsOrderService;
import com.wang.mall.util.AlipayTemplate;
import com.wang.mall.vo.PayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PayController {


    @Autowired
    private AlipayTemplate alipayTemplate;

    @Autowired
    private OmsOrderService omsOrderService;

    /**
     * xgwupg6671@sandbox.com  支付账号,
     * 支付成功后要跳到用户的订单列表页
     *
     * @param orderSn
     * @return
     * @throws AlipayApiException
     */
    @GetMapping(value = "/payOrder", produces = "text/html")
    public String pay(@RequestParam("orderSn") String orderSn) throws AlipayApiException {
        // 查询订单状态
        OmsOrder omsOrder = omsOrderService.getOne(new LambdaQueryWrapper<OmsOrder>().eq(OmsOrder::getOrderSn, orderSn));
        if (omsOrder == null || omsOrder.getStatus() != 0) {
            // 订单状态不是待付款,不能支付
            return "500";
        }

        PayVo payVo = omsOrderService.getOrderPay(orderSn);
        String pay = alipayTemplate.pay(payVo);
        return pay;
    }

}
